﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace RestaurantManagerWebApp.Pages.TableManagement
{
    public partial class TableAllocation : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
<<<<<<< .mine
            MultiView1.SetActiveView(allocateView);
=======
            generateButtons();
>>>>>>> .r89
        }

        protected void buttonManageTables_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/TableManagement/TableManagement.aspx");
        }

        protected void buttonAllocateTables_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/TableManagement/TableAllocation.aspx");
        }

        protected void buttonReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/TableManagement/MainTableManagement.aspx");
        }

        protected void tableGridview_DataBound(object sender, EventArgs e)
        {
            foreach (GridViewRow row in tableGridview.Rows)
                if (row.Cells[1].Text == "Available")
                {
                    row.BackColor = System.Drawing.Color.DarkGreen;
                }
        }

        protected void tableGridview_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void tableGridview_RowDataBound(object sender, GridViewRowEventArgs e)
        {
             
        }
<<<<<<< .mine
         
=======

        protected void btnSwitch_Click(object sender, EventArgs e)
        {
            string activeMV = MultiView1.GetActiveView().ID;

            if (activeMV == overviewView.ID)
            {
                MultiView1.SetActiveView(allocateView);
                activeMV = MultiView1.GetActiveView().ID;
            }
            else
            {
                MultiView1.SetActiveView(overviewView);
                activeMV = MultiView1.GetActiveView().ID;
            }
        }

        private void generateButtons()
        {
            
            //SqlCommand cmd = new SqlCommand("SELECT * FROM Tables;", conn);
            SqlCommand cmd = new SqlCommand("SELECT table_no, status, capacity, t_category FROM Tables WHERE (status <> 'NIL') ORDER BY CAST(table_no AS decimal);", conn);
            cmd.Connection = conn;
            conn.Open();

            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                string tableNo = reader["table_no"].ToString();
                string status = reader["status"].ToString();
                string cap = reader["capacity"].ToString();
                string cat = reader["t_category"].ToString();

                Button btnTable = new Button();
                btnTable.ID = tableNo;
                if (status == "Available")
                { btnTable.BackColor = System.Drawing.Color.Green; }
                else
                { btnTable.BackColor = System.Drawing.Color.Red; }
                btnTable.Width = 100;
                btnTable.Height = 45;
                btnTable.Text = "Table: " + tableNo + "\nCapacity: " + cap;
                btnTable.Font.Name = "Calibri";
                btnTable.Font.Size = 13;

                btnTable.Click += new EventHandler(generateButtonsClick);
                phAutoGenBtn.Controls.Add(btnTable);
            }
            conn.Close();
        }

        private void generateButtonsClick(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string table_no = btn.ID;

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            conn.Open();

            if (btn.BackColor == System.Drawing.Color.Green)
            {
                cmd.CommandText = "Update Tables set status = 'Not Available' where table_no = @table_no;";
                cmd.Parameters.AddWithValue("@table_no", table_no);
                int affectedRows = cmd.ExecuteNonQuery();
                conn.Close();
            }
            else
            {
                cmd.CommandText = "Update Tables set status = 'Available' where table_no = @table_no;";
                cmd.Parameters.AddWithValue("@table_no", table_no);
                int affectedRows = cmd.ExecuteNonQuery();
                conn.Close();
            }

            Response.Redirect("~/Pages/TableManagement/TableAllocation.aspx");
        }

>>>>>>> .r89
    }
}